package com.youth.youthservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.youth.commonutils.BeanCopyUtils;
import com.youth.commonutils.R;
import com.youth.youthservice.entity.EduCourse;
import com.youth.youthservice.entity.EduSubject;
import com.youth.youthservice.entity.vo.CourseVo.SubjectParam;
import com.youth.youthservice.entity.vo.CourseVo.SubjectVo4Publish;
import com.youth.youthservice.mapper.EduSubjectMapper;
import com.youth.youthservice.service.EduCourseService;
import com.youth.youthservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author CM，WYZ
 * @since 2023-4-27
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    @Autowired
    private EduCourseService courseService;


    @Override
    public R getAllSubject4Publish() {
        /*先查询父级课程*/
        LambdaQueryWrapper<EduSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduSubject::getParentId, 0).orderByAsc(EduSubject::getSort);
        List<EduSubject> eduSubjectList = list(queryWrapper);
//        封装为vo对象并填充子类
        List<SubjectVo4Publish> res = castSubjectVo(eduSubjectList);
        return R.ok().data("list", res);
    }

    /*获取所有一级科目信息*/
    @Override
    public R getOneLevelSubjects() {
//        使用sql查询
        List<SubjectParam> oneLevelSubjects = baseMapper.getOneLevelSubjects();
        return R.ok().data("oneLevelSubjects", oneLevelSubjects);
    }

    /*新增科目*/
    @Override
    public R addSubject(SubjectParam subjectParam) {
        EduSubject subject = new EduSubject();
        String parentId = subjectParam.getParentId();
        if(!StringUtils.isEmpty(parentId)){
            /*说明添加的是二级科目，添加parentId*/
            subject.setParentId(parentId);
        }
        subject.setSort(subjectParam.getSort());
        subject.setTitle(subjectParam.getTitle());
        save(subject);
        return R.ok();
    }

    @Override
    public R removeSubject(String id, String parentId) {
        if(StringUtils.isEmpty(id) || StringUtils.isEmpty(parentId)){
            return R.error().message("参数故障");
        }
//        检查是否是父级id
        if("0".equals(parentId)) {
//               检查所有子科目是否有绑定课程
            LambdaQueryWrapper<EduSubject> subjectWrapper = new LambdaQueryWrapper<>();
            subjectWrapper.eq(EduSubject::getParentId,id);
//            查询所有子科目id
            List<String> childrenIds = baseMapper.listChildrenIds(id);
            for (String childrenId : childrenIds) {
                if(isBindSubject(childrenId)){
                    return R.error().message("已绑定课程,无法删除").code(0);
                }
            }
//            没有出现已绑定课程的id，可以删除
            childrenIds.add(id);
            removeByIds(childrenIds);
        }
//       检查子id是否能删除
        if(!isBindSubject(id)){
            removeById(id);
            return R.ok().code(1);
        }
        return R.error().message("已绑定课程,无法删除").code(0);
    }

//    判定是否有课程绑定
    private Boolean isBindSubject(String subjectId){
        //        先检查是否有课程绑定在该科目上
        LambdaQueryWrapper<EduCourse> courseWrapper = new LambdaQueryWrapper<>();
        courseWrapper.eq(EduCourse::getSubjectId,subjectId);
        return courseService.count(courseWrapper) != 0;
    }


    private List<SubjectVo4Publish> castSubjectVo(List<EduSubject> source) {
//        封装为vo对象
        List<SubjectVo4Publish> target = BeanCopyUtils.copyBeanToList(source, SubjectVo4Publish.class);
//        封装子级课程
        target = target.stream().map(vo -> {
            vo.setChildren(getChildrenSubjects(vo.getId()));
            return vo;
        }).collect(Collectors.toList());
        return target;
    }

    private List<SubjectVo4Publish> getChildrenSubjects(String parentId) {
        LambdaQueryWrapper<EduSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduSubject::getParentId, parentId).orderByAsc(EduSubject::getSort);
//        封装子类为VO对象
        List<EduSubject> list = list(queryWrapper);
        List<SubjectVo4Publish> children = BeanCopyUtils.copyBeanToList(list, SubjectVo4Publish.class);
        return children;
    }
}
